package com.atguigu.edustatistic.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.edustatistic.client.UcenterClient;
import com.atguigu.edustatistic.entity.StatisticsDaily;
import com.atguigu.edustatistic.mapper.StatisticsDailyMapper;
import com.atguigu.edustatistic.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-04-20
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {
        //远程调用
        R r = ucenterClient.countRegister(day);
        Integer register = (Integer) r.getData().get("countRegister");

        //加之前判断数据库中是否有相同日期的数据 如果有就删除
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        //获取统计信息
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO

        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(register);
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setLoginNum(loginNum);
        statisticsDaily.setVideoViewNum(videoViewNum);
        statisticsDaily.setCourseNum(courseNum);

        //把数据添加到统计分析表中
        baseMapper.insert(statisticsDaily);
    }

    //图表显示 返回日期的json数组 和 数据的json数组
    @Override
    public Map<String, Object> showdata(String type, String begin, String end) {
        //根据条件查询数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.select("date_calculated",type);
        wrapper.between("date_calculated",begin,end);
        List<StatisticsDaily> selectList = baseMapper.selectList(wrapper);
        //分别封装日期和日期对应的数据
        List<String> date = new ArrayList<>();
        List<String> dateData = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        for (StatisticsDaily item:selectList){
            date.add(item.getDateCalculated());
            switch (type){
                case "login_num":
                    dateData.add(item.getLoginNum()+"");
                    break;
                case "register_num":
                    dateData.add(item.getRegisterNum()+"");
                    break;
                case "video_view_num":
                    dateData.add(item.getVideoViewNum()+"");
                    break;
                case "course_num":
                    dateData.add(item.getCourseNum()+"");
                    break;
                default:
                    break;
            }
        }
        map.put("date", date);
        map.put("dateData",dateData);
        return map;
    }
}
